package com.lanou.sfu.songforu.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.sfu.songforu.base.BaseApplication;

/**
 * Created by dllo on 15/9/28.
 */
public class DaoSingleton {

    private static final String DATABASE_NAME = "SongForU.db";
    private volatile static DaoSingleton instance;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private SongListEntityDao songListEntityDao;
    private BillboardEntityDao billboardEntityDao;

    private DaoSingleton() {
        context = BaseApplication.getAppContext();
    }

    public static DaoSingleton getInstance() {
        if (instance == null) {
            synchronized (DaoSingleton.class) {
                if (instance == null) {
                    instance = new DaoSingleton();
                }
            }
        }
        return instance;


    }

    public DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }

        return helper;
    }

    private SQLiteDatabase getDb(){
        if (db == null){
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    private DaoMaster getDaoMaster(){
        if (daoMaster == null){
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession(){
        if (daoSession == null){
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public SongListEntityDao getSongListEntityDao(){
        if (songListEntityDao == null){
            songListEntityDao = getDaoSession().getSongListEntityDao();
        }
        return songListEntityDao;
    }

    public BillboardEntityDao getBillboardEntityDao(){
        if (billboardEntityDao == null){
            billboardEntityDao = getDaoSession().getBillboardEntityDao();
        }
        return billboardEntityDao;
    }

}